/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controladores;

import Interfaces.ICtrlPersistencia;
import Proxies.DataHistorial;
import Proxies.DataMensaje;
import Proxies.DataUsuario;
import Proxies.DataUsuarioArray;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sebas
 */
public class CtrlPersistencia{

    public CtrlPersistencia() {
    }
    
//    @Override
    public void guardarDatos(DataUsuarioArray listU) {
        
        if (!(new File("C:\\DispMovil\\")).exists()) {
            (new File("C:\\DispMovil\\")).mkdir();
        }
        
       CsvWriter writer = new CsvWriter("C:\\DispMovil\\Memoria.csv");

        try {
//writer.write(String) - Escribe los campos separandolos con coma automaticamente.
            writer.write("Correo");
            writer.write("Nick");
            writer.write("Imagen");
            
//writer.endRecord() - Termina con la linea.

           
            Iterator<DataUsuario> it = listU.getItem().iterator();
            DataUsuario da;
            while (it.hasNext()) {
                da = it.next();
                writer.endRecord();
                writer.write(da.getCorreo());
                writer.write(da.getNick());
                writer.write(da.getImagen());
            }

        } catch (IOException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        } finally {

            writer.close();
        }
 
}

//    @Override
    public void cargarDatos() {
        
       DataUsuarioArray list = new DataUsuarioArray();
        
        try {
            CsvReader amigos = new CsvReader(new InputStreamReader(new FileInputStream("C:\\DispMovil\\Memoria.csv"), "UTF-8"));
              
            amigos.readHeaders();

               while (amigos.readRecord()) {
                   String correo = amigos.get("Correo");
                   String nick = amigos.get("Nick");
                   String imagen = amigos.get("Imagen");
                   DataUsuario dt = new DataUsuario();
                   dt.setCorreo(correo);
                   dt.setNick(nick);
                   dt.setImagen(imagen);
                   list.getItem().add(dt);
               }
               
               amigos.close();
               Memoria.getInstance().setAmigos(list);
               
        } catch (IOException ex) {
            Logger.getLogger(CtrlPersistencia.class.getName()).log(Level.SEVERE, null, ex);
     
        }
    
    }
    
    public void guardarHistoriales(HashMap<String,DataHistorial> hist){
        
         if (!(new File("C:\\DispMovil\\")).exists()) {
            (new File("C:\\DispMovil\\")).mkdir();
        }
        
       CsvWriter writer = new CsvWriter("C:\\DispMovil\\Historial.csv");

       
            try {
                
                 writer.write("idAmigo");
                 writer.write("emisor");
                 writer.write("msj");
                 
                 Iterator <DataHistorial>it = hist.values().iterator();
                 
                 while(it.hasNext()){
                     
                     DataHistorial dhis = it.next();
                     Iterator<DataMensaje>itMen = dhis.getMensajes().iterator();

                     while(itMen.hasNext()){
                         writer.endRecord();
                         writer.write(dhis.getIdAmigo());
                         DataMensaje dmen = itMen.next();
                         writer.write(dmen.getEmisor());
                         writer.write(dmen.getMsj());
                     }
                     
                     
                 }
                 writer.close();
            } catch (IOException ex) {
                Logger.getLogger(CtrlPersistencia.class.getName()).log(Level.SEVERE, null, ex);
            }
        
    }
    
}
        
       